var app = getApp();
Page({
    data:{
        page : {
            isend : false,
            doing : false,
            doo : 'agent',
            pdata : {
                op : 'orderlist',
                page : 1,
                status : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        tabMenu :  [{
            name: "全部",
            status: 0
        }, {
            name: "已结算",
            status: 1
        }, {
            name: "未结算",
            status: 2
        }, {
            name: "已失效",
            status: 3
        }],
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
		
		var shopid = options.shopid ? options.shopid : '';
        
        var zfid = options.zfid ? options.zfid : '';
        
        self.setData({
            'page.pdata.status' : options.status ? options.status : 0,
            'page.pdata.type' : options.type ? options.type : 0,
            'page.pdata.zfid' : zfid,
            'page.pdata.shopid' : shopid,
        });

        app.com.auth(function(user){
			
            app.com.getPage(self,self.data.page,20,function(res){
                var data = res.data.data;
                self.setData({
                   datalist : data.list,
                   Pagetheme : res.data.data.params.style,
                   sharedata : res.data.data.sharedata,
                },function(){
                    self.setData({
                        inited : true,
                    });
                });

            },false,function(res){
                app.com.alert(res.data.message);
            });
			app.com.setBar(app,self,shopid);
        });
       
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    tabChange(e){
        var self = this;

        if( self.data.ising || ( e.target.dataset.status < 3 && e.target.dataset.status == self.data.page.pdata.status ) ) return false;
        self.data.ising = true;

        self.setData({
            datalist : [],
            'page.pdata.status' : e.currentTarget.dataset.status,
            'page.pdata.page' : 1,
            'page.isend' : false,
            'page.waitf' : 0,
            'page.nodataf' : 0
        });

        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : res.data.data.list,
            });
            wx.pageScrollTo({
                scrollTop: 0
            });
        });

        self.data.ising = false;
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    getRefund(e){
        var self = this;
        
        
        var postdata = {
            op:'applyback',
            oid : e.currentTarget.dataset.id,
        };

        app.com.confirm('确定要申请退款吗？',function(){
            app.com.http('order','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    for (var i = 0; i < self.data.datalist.length; i++) {
                        if( self.data.datalist[i].id == postdata.oid ){
                            self.setData({
                                ['datalist['+i+'].isback'] : 1,
                            })
                        }
                    }
                }else{
                    app.com.alert(res.data.message);
                }
            });
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },    

})